package shop.web.controller.front.account;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.AccessToken;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.WxUrlType;
import shop.core.util.WxUtil;
import shop.services.front.account.AccountService;
import shop.services.front.account.bean.Account;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/17.
 */
@Controller
@RequestMapping("/account")
public class AccountController extends FrontBaseController<Account> {
    @Resource
    private AccountService accountService;
    @Override
    public Services<Account> getService() {
        return accountService;
    }


    /**
     * 页面跳转方法
     *
     * @return
     */
    @RequestMapping("/toAccountList")
    public String toAccountList() {
        return "/front/account/accountList";
    }

    @RequestMapping("/toAccountOrganizeList")
    public String toAccountOrganizeList() {
        return "/front/account/accountOrganizeList";
    }

    @RequestMapping("/toAccountOfflineList")
    public String toAccountOfflineList() {
        return "/front/account/accountOfflineList";
    }

    @RequestMapping("/toAccountGroupList")
    public String toAccountGroupList() {
        return "/front/account/accountGroupList";
    }

    @RequestMapping("/toAccountBindPhone")
    public String toAccountBindPhone() {
        return "/front/account/accountBindPhone";
    }
    @RequestMapping("/toAccountRank")
    public String toAccountRank(){return "/front/account/accountRank";}


    /**
     * 登录成功跳转
     * @param request
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginSys(HttpServletRequest request){
        String code = request.getParameter("code");
        Map<String,String> params = new HashMap<String,String>();
        params.put("appid","wx9dcac60230d12fbe");
        params.put("secret","ec1a0af8831a96ea8f890431bd12d809");
        params.put("code",code);
        params.put("grant_type","authorization_code");
        AccessToken accessToken = WxUtil.sendRequest(WxUrlType.accessTokenUrl, HttpMethod.GET,params,null, AccessToken.class);
        Map<String,String> getUserInfoParams = new HashMap<String,String>();
        getUserInfoParams.put("access_token", accessToken.getAccess_token());
        getUserInfoParams.put("openid", accessToken.getOpenid());
        getUserInfoParams.put("lang","zh_CN");
        Account account = WxUtil.sendRequest(WxUrlType.userInfoUrl, HttpMethod.GET, getUserInfoParams, null, Account.class);

        return "redirect:/front/account/accountList";
    }

    /**
     * 微信登录
     * @return
     */
    @RequestMapping(value = "/go", method = RequestMethod.GET)
    public String  goWx(){
        String appidUrl="?appid=&";
        String redirect_uri= "redirect_uri=http://www.kekelvxing.cn/login&";
        String typeUrl ="response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        return "redirect:"+WxUrlType.authorizeUrl+appidUrl+redirect_uri+typeUrl;
    }

    /**
     * 查询捐助排行
     * @param account
     * @return
     */
    @RequestMapping(value = "/selectRank",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectRank(Account account){
        JSONResult rs=new JSONResult();
        List<Account> accountList= accountService.selectList(account);
        for (int i=0;i<accountList.size();i++){
            if (accountList.get(i).getId().equals(account.getId())){
                accountList.get(i).setAccountRank(i+1);
            }
        }
        rs.setData(accountList);
        rs.setSuccess(true);
        return  rs;
    }

    /**
     * 单个查询用户
     * @param account 用户
     */
    @RequestMapping(value = "/selectOne",method =RequestMethod.GET )
    @ResponseBody
    public JSONResult selectOne(Account account){
        JSONResult rs =new JSONResult();
        rs.setData(accountService.selectOne(account));
        rs.setSuccess(true);
        return rs;
    }
}